﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Entity;
namespace Web.Controllers
{
    using Data;
    using Entity;
    using Data.TEST;
    using Entity.TEST;
    using Web;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    public class HomeController : Controller
    {

        IUserRepository iUserRepository = null;
        Repository<Web_ExceptionLog> iRepository = null;
        public HomeController()
        {
            iUserRepository = new UserRepository();
            iRepository = new Repository<Web_ExceptionLog>();
        }
        public ActionResult Index()
        {
            HttpCookie cookie = new HttpCookie("json", "{ id = 1, name ='zzl', tel = '13521972991' }");
            HttpContext.Response.Cookies.Add(cookie);
            List<UserBases> model = iUserRepository.GetModel().Cast<UserBases>().ToList();
            return View(model);
        }
        public ActionResult Create()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Create(FormCollection form)
        {
            UserBases entity = new UserBases();
            TryUpdateModel(entity);
            entity.UserID = Guid.NewGuid().ToString();
            entity.CreateDate = DateTime.Now;
            entity.UpdateDate = DateTime.Now;
            entity.Status = 100;
            if (entity.IsValid)
            {
                iUserRepository.RegisterUser(entity);
            }
            else
            {
                entity.GetRuleViolations().ToList().ForEach(i =>
                 {
                     ModelState.AddModelError(i.PropertyName, i.ErrorMessage);
                 });
            }

            return View();
        }

        public ActionResult Edit(string id)
        {
            UserBases entity = (UserBases)iUserRepository.Find(id);
            return View(entity);
        }
        [HttpPost]
        public ActionResult Edit(FormCollection form)
        {
            UserBases entity = (UserBases)iUserRepository.Find(form["UserID"]);
            //entity.OnPropertyChange(); //先订阅,放到了实体初始化时去订阅
            TryUpdateModel(entity);//再等待事件被触发,TryUpdateModel中有对属性的set操作，事件就是从这里被触发的
            if (entity.IsValid)
            {
                entity.UpdateDate = DateTime.Now;
                iUserRepository.Update(entity);
                return RedirectToAction("Index");
            }
            else
            {
                entity.GetRuleViolations().ToList().ForEach(i =>
                 {
                     ModelState.AddModelError(i.PropertyName, i.ErrorMessage);
                 });

                return View();
            }

        }

        public ActionResult Delete(string id)
        {
            iUserRepository.Delete(new UserBases { UserID = id });
            return RedirectToAction("Index");
        }

        public ActionResult Details(string id)
        {
            UserBases entity = (UserBases)iUserRepository.Find(id);
            return View(entity);
        }

        public JsonResult DelMore(string idArr)
        {
            bool flag = false;
            idArr.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList().ForEach(i =>
            {
                iUserRepository.Delete(new UserBases { UserID = i });
            });
            return Json(new { success = flag });
        }
    }
}
